package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.RentMapper;
import com.farmpost.server.pojo.Rent;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.service.IRentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 租房表 服务实现类
 * </p>
 *
 * @author king
 * @since 2022-04-08
 */
@Service
public class RentServiceImpl extends ServiceImpl<RentMapper, Rent> implements IRentService {

    @Autowired
    private RentMapper rentMapper;

    /**
     * 发布租房/出租信息
     *
     * @param rent
     * @return
     */
    @Override
    public RespBean insertRent(Rent rent) {
        rent.setDateTime(LocalDateTime.now());
        int insert = rentMapper.insert(rent);
        if (insert == 1)
            return RespBean.success("发布租房/出租信息成功");
        return RespBean.error("发布租房/出租信息失败");
    }

    /**
     * 查询出租信息
     *
     * @return
     */
    @Override
    public List<Rent> getRentByLease() {
        List<Rent> lease = rentMapper.selectList(new QueryWrapper<Rent>().eq("rent_or_lease", 1));
        return setFiles(lease);
    }

    /**
     * 查询租房信息
     * @return
     */
    @Override
    public List<Rent> getRentByRent() {
        List<Rent> rents = rentMapper.selectList(new QueryWrapper<Rent>().eq("rent_or_lease", 0));
        return setFiles(rents);
    }

    /**
     * 修改租房信息
     * @param rent
     * @return
     */
    @Override
    public RespBean updateRent(Rent rent) {
        int update = rentMapper.update(rent, new UpdateWrapper<Rent>().eq("id", rent.getId()));
        if (update == 1)
            return RespBean.success("修改租房信息成功");
        return RespBean.error("修改租房信息失败");
    }

    /**
     * 根据id删除租房信息
     * @param id
     * @return
     */
    @Override
    public RespBean deleteRent(Integer id) {
        int delete = rentMapper.delete(new QueryWrapper<Rent>().eq("id", id));
        if (delete == 1)
            return RespBean.success("删除租房信息成功");
        return RespBean.error("删除租房信息失败");
    }

    /**
     * 查询所有租房信息
     * @param rentPage
     * @return
     */
    @Override
    public List<Rent> getAllRent(Page<Rent> rentPage) {
        QueryWrapper<Rent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",0);
        List<Rent> list = rentMapper.selectPage(rentPage, queryWrapper).getRecords();
        return setFiles(list);
    }

    private List<Rent> setFiles(List<Rent> lease) {
        for (Rent rent : lease) {
            if (rent.getIsEntireRent())
                rent.setDescEntireRent("整租");
            else
                rent.setDescEntireRent("合租");
            if (rent.getRentOrLease())
                rent.setDescRent("出租");
            else
                rent.setDescRent("租房");
        }
        return lease;
    }

}
